﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using FirebirdSql.Data.FirebirdClient;
using ZedGraph;

namespace University
{
    public partial class VNADZVUCHAJNI : University.VUDATKY
    {
        string planed;
        string real;
        int n;
        string field1, field2;
        Main mForm = new Main();
        List<string> nvudatok = new List<string>();
        List<string> nyear= new List<string>();
        List<string> tvudatok = new List<string>();
        public VNADZVUCHAJNI()
        {
            InitializeComponent();
        }
        public VNADZVUCHAJNI(string a, string p, string s, int t, string z,string r)
            : base(a, p, s, t)
        {
            n = t;
            planed = z;
            real = r;
            InitializeComponent();
        }
        private void vnadzvuchajni(string data)
        {
            nyear.Clear();
            nvudatok.Clear();

            String[] el1 = data.Split('\n');
            string data1="";
            if(el1.Length>5)
            {
                for (int i = (el1.Length - 5); i <= el1.Length - 1; i++)
                {
                    if (i != el1.Length - 1)
                    { data1 += el1[i] + '\n'; }
                    else
                    { data1 += el1[i]; }
                }
                data = data1;
            }

           
            /* 
             *   String[] el1 = data.Split('\n');
            int nom = el1.Length;while (nom > 5)
               {
                  data=data.Remove(0, el1[0].Length+1);
                
                   FbConnection con3 = new FbConnection(mForm.str);
                   con3.Open();
                   if (col == 1)
                   {
                       FbCommand sqlup4 = new FbCommand("UPDATE VNADZVUCHAJNI SET ZAPLANOVANI=@ZAPLANOVANI WHERE UNIQUE_NO = @UNIQUE_NO", con3);
                       sqlup4.Parameters.AddWithValue("UNIQUE_NO", n);
                       sqlup4.Parameters.AddWithValue("ZAPLANOVANI", data);
                       sqlup4.ExecuteNonQuery();
                   }
                   if (col == 2)
                   {
                       FbCommand sqlup5 = new FbCommand("UPDATE VNADZVUCHAJNI SET REALNI=@REALNI WHERE UNIQUE_NO = @UNIQUE_NO", con3);
                       sqlup5.Parameters.AddWithValue("UNIQUE_NO", n);
                       sqlup5.Parameters.AddWithValue("REALNI", data);
                       sqlup5.ExecuteNonQuery();
                   }
                   con3.Close();
                  nom--;
               }
               */
            String[] el = data.Split('\n');
            if (el[0] != "")
            {
                for (int i = 0; i < el.Length; i++)
                {
                    nyear.Add(el[i].Substring(0, 4));
                    nvudatok.Add(el[i].Substring(el[i].IndexOf(' ') + 1, el[i].Length - el[i].IndexOf(' ') - 1));
                }
            }
        }
        public virtual void GraphCreate(ZedGraphControl zg1,string title,int color)
        {
            GraphPane myPane;
            myPane = zg1.GraphPane;
            myPane.Title.Text = title;
            myPane.YAxis.Title.Text = "P,grn";
            string s = comboRik.Text;
            myPane.XAxis.Title.Text = "Роки";

           double[] a = new double[nvudatok.Count];
            string[] b = new string[nyear.Count];
            for (int i = 0; i < nvudatok.Count; i++)
                a[i] = Convert.ToDouble(nvudatok[i]);
            for (int i = 0; i < nyear.Count; i++)
                b[i] = nyear[i];

            BarItem curve;
            if(color==1)
            curve = myPane.AddBar("Graphic", null, a, Color.Orange);
            if(color==2)
            curve = myPane.AddBar("Graphic", null, a, Color.Green);
            if(color==3)
             curve = myPane.AddBar("Graphic", null, a, Color.Red);
         
            myPane.XAxis.Type = AxisType.Text;
            BarItem.CreateBarLabels(myPane, false, "f0");
            myPane.XAxis.Scale.TextLabels = b;

            zg1.AxisChange();
            zg1.Invalidate();


        }
        private void buttonReal_Click(object sender, EventArgs e)
        {
            vnadzvuchajni(real);
            zedGraphVNadz.GraphPane.CurveList.Clear();
            zedGraphVNadz.GraphPane.GraphObjList.Clear();
            GraphCreate(zedGraphVNadz, "Реальні надзвичайні видатки",1);
        }

        private void buttonPlaned_Click(object sender, EventArgs e)
        {
            vnadzvuchajni(planed);
            zedGraphVNadz.GraphPane.CurveList.Clear();
            zedGraphVNadz.GraphPane.GraphObjList.Clear();
            GraphCreate(zedGraphVNadz, "Заплановані надзвичайні видатки",2);
        }

        private void buttonDiff_Click(object sender, EventArgs e)
        {
            tvudatok.Clear();
            vnadzvuchajni(planed);
            for (int i = 0; i < nyear.Count; i++)
            {
                tvudatok.Add(nvudatok[i]);
            }
            vnadzvuchajni(real);
            for (int i = 0; i < nvudatok.Count; i++)
            {
                nvudatok[i] = Convert.ToString(Convert.ToDouble(tvudatok[i]) - Convert.ToDouble(nvudatok[i]));
            }
            zedGraphVNadz.GraphPane.CurveList.Clear();
            zedGraphVNadz.GraphPane.GraphObjList.Clear();
            GraphCreate(zedGraphVNadz, "Різниця між запланованими та реальними надзвичайними видатками",3);
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            vnadzvuchajni(real);
          if(nyear.Count!=0)
            labelRikVN.Text = Convert.ToString(Convert.ToInt32(nyear[nyear.Count - 1]) + 1);
            else
              labelRikVN.Text = System.DateTime.Today.Year.ToString();
 
            groupBoxAddVN.Visible = true;
            buttonVN.Enabled = false;
        }

        private void buttonAddVN_Click(object sender, EventArgs e)
        {
            

            if ( (textBoxRVN.Text != "") && (textBoxZVN.Text != ""))
            {
               
                if (planed != "")
                    field1 = planed + '\n' + labelRikVN.Text + " " + textBoxZVN.Text;
                else
                    field1 = labelRikVN.Text + " " + textBoxZVN.Text ;
                

                if (real != "")
                    field2 = real + '\n' + labelRikVN.Text + " " + textBoxRVN.Text;
                else
                    field2 = labelRikVN.Text + " " + textBoxRVN.Text ;
             

                FbConnection con2 = new FbConnection(mForm.str);
                con2.Open();

                FbCommand sqlup = new FbCommand("UPDATE VNADZVUCHAJNI SET ZAPLANOVANI=@ZAPLANOVANI WHERE UNIQUE_NO = @UNIQUE_NO", con2);
                sqlup.Parameters.AddWithValue("UNIQUE_NO", n);
                sqlup.Parameters.AddWithValue("ZAPLANOVANI", field1);
                sqlup.ExecuteNonQuery();

                FbCommand sqlup1 = new FbCommand("UPDATE VNADZVUCHAJNI SET REALNI=@REALNI WHERE UNIQUE_NO = @UNIQUE_NO", con2);
                sqlup1.Parameters.AddWithValue("UNIQUE_NO", n);
                sqlup1.Parameters.AddWithValue("REALNI", field2);
                sqlup1.ExecuteNonQuery();


                FbCommand cmd1 = new FbCommand(@"SELECT * FROM VNADZVUCHAJNI  where UNIQUE_NO=" + n, con2);
                FbDataReader rdr1 = cmd1.ExecuteReader();
                rdr1.Read();

                planed = rdr1["ZAPLANOVANI"].ToString();
                real = rdr1["REALNI"].ToString();
               
                rdr1.Close();
                con2.Close();

                labelRikVN.Text = "";
                textBoxRVN.Text = "";
                textBoxZVN.Text = "";

                buttonVN.Enabled = true;
                groupBoxAddVN.Visible = false;
               
            }
            else
            { MessageBox.Show("Заповніть всі поля!"); }

            
        }
    }
}
